home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr11 / pctv4n2.zip / B40.C next >
C/C++ Source or Header  |  1993-06-09  |  3KB  |  109 lines

  1. /***************************************************************
  2.  *                                                             *
  3.  * B40.C - Base 40 compression/decompression routines          *
  4.  * by Al Williams                                              *
  5.  *                                                             *
  6.  ***************************************************************/
  7.  
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. /* default ASCII to B40 table (100=no mapping) */
  12. char _b40_ascb40[256]=
  13.   {
  14.   0  ,100,100,100,100,100,100,100,
  15.   100,100,100,100,100,100,100,100,
  16.   100,100,100,100,100,100,100,100,
  17.   100,100,100,100,100,100,100,100,
  18.   39, 100,100,100,100,100,100,100,
  19.   100,100,100,100, 38,100, 37,100,
  20.   27,  28, 29, 30, 31, 32, 33, 34,
  21.   35,  36,100,100,100,100,100,100,
  22.   100,  1,  2,  3,  4,  5,  6,  7,
  23.   8,    9, 10, 11, 12, 13, 14, 15,
  24.   16,  17, 18, 19, 20, 21, 22, 23,
  25.   24,  25, 26,100,100,100,100,100,
  26.   100,100,100,100,100,100,100,100,
  27.   100,100,100,100,100,100,100,100,
  28.   100,100,100,100,100,100,100,100,
  29.   100,100,100,100,100,100,100,100,
  30.   100,100,100,100,100,100,100,100,
  31.   100,100,100,100,100,100,100,100,
  32.   100,100,100,100,100,100,100,100,
  33.   100,100,100,100,100,100,100,100,
  34.   100,100,100,100,100,100,100,100,
  35.   100,100,100,100,100,100,100,100,
  36.   100,100,100,100,100,100,100,100,
  37.   100,100,100,100,100,100,100,100,
  38.   100,100,100,100,100,100,100,100,
  39.   100,100,100,100,100,100,100,100,
  40.   100,100,100,100,100,100,100,100,
  41.   100,100,100,100,100,100,100,100,
  42.   100,100,100,100,100,100,100,100,
  43.   100,100,100,100,100,100,100,100,
  44.   100,100,100,100,100,100,100,100,
  45.   100,100,100,100,100,100,100,100
  46.   };
  47.  
  48. /* pointer to ASCII to B40 table */
  49. char *b40_ascb40=_b40_ascb40;
  50.  
  51. /* default B40 to ASCII table */
  52. char _b40_b40asc[40]="\0ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789., ";
  53.  
  54. /* pointer to B40 to ASCII table */
  55. char *b40_b40asc=_b40_b40asc;
  56.  
  57.  
  58. /*******************************************************/
  59. /* Encode string (src) to B40 string (dstin)
  60.    returns length of encoded string or -1 for error */
  61. int b40_encode(char *dstin,char *src)
  62.   {
  63.   unsigned short twobytes=0;
  64.   unsigned char c[3];
  65.   char *dst=dstin;
  66.   do
  67.     {
  68. /* Collect 3 characters -- pad with 0's at end of string */
  69.     c[0]=b40_ascb40[*src];
  70.     if (c[0]) c[1]=b40_ascb40[*++src]; else c[1]='\0';
  71.     if (c[1]) c[2]=b40_ascb40[*++src]; else c[2]='\0';
  72.  
  73. /* If any character not supported, return error */
  74.     if (c[0]==100||c[1]==100||c[2]==100)
  75.       return -1;
  76.  
  77. /* compute 2 byte B40 number */
  78.     twobytes=c[0]*1600+c[1]*40+c[2];
  79. /* store in destination */
  80.     *((unsigned short *)dst)++=twobytes;
  81.     } while (*src++);
  82. /* return length */
  83.   return dst-dstin;
  84.   }
  85.  
  86. /*******************************************************/
  87. /* Decode B40 string (src) of length n to ASCII (dst) */
  88. int b40_decode(char *dst,char *src,int n)
  89.   {
  90.   unsigned short twobytes=0;
  91.   unsigned char c[3];
  92.   do
  93.     {
  94.     int tmp;
  95. /* read two bytes from src */
  96.     twobytes=*((unsigned short *)src)++;
  97. /* split 3 B40 "digits" and decode from two byte code */
  98.     c[2]=b40_b40asc[tmp=twobytes%40];
  99.     c[1]=b40_b40asc[(tmp=((twobytes-tmp)%1600))/40];
  100.     c[0]=b40_b40asc[(twobytes-tmp)/1600];
  101. /* move to destination */
  102.     memcpy(dst,c,3);
  103.     dst+=3;
  104.     n-=2;
  105.     } while (n>0);       /* repeat for whole string */
  106.   return 0;
  107.   }
  108.  
  109.